QEMU_PATH:=$(NATIVE_CLIENT_PATH)/toolchain/linux_x86/arm_trusted/
SCONS_HOST_ARM :=$(NATIVE_CLIENT_PATH)/scons-out/dbg-linux-arm/staging
SCONS_NACL_ARM :=$(NATIVE_CLIENT_PATH)/scons-out/nacl_irt-arm/staging

QEMU := $(QEMU_PATH)/run_under_qemu_arm
SEL_LDR := $(SCONS_HOST_ARM)/sel_ldr
BOOTSTRAP:= $(SCONS_HOST_ARM)/nacl_helper_bootstrap
STD_ARGS := --r_debug=0xXXXXXXXXXXXXXXXX --reserved_at_zero=0xXXXXXXXXXXXXXXXX
STD_ARGS += -a -c -c -Q -B $(SCONS_NACL_ARM)/irt_core.nexe

RUN_STATIC_CMD := $(QEMU) $(BOOTSTRAP) $(SEL_LDR) $(STD_ARGS)
RUN_DYNAMIC_CMD := $(QEMU) $(BOOTSTRAP) $(SEL_LDR) $(STD_ARGS) $(DYN_LDR) -v -v -v

all:
run: static_tests dynamic_tests

LIBC:=$(TOOLCHAIN_PATH)/arm-nacl/lib/libc.a

.PHONY: prep_libc
prep_static:
	@echo "**************************************************************"
	@echo "Rebuild libc. : $(LIBC)"
	@echo "**************************************************************"
	make -C ../libc -j20

.PHONY: prep_dynamic
prep_dynamic : prep_libc
	@echo "**************************************************************"
	@echo "Rebuild linker."
	@echo "**************************************************************"
	make -C ../newlinker -j20
	@echo "**************************************************************"
	@echo "Copy libraries."
	@echo "**************************************************************"
	cp $(TOOLCHAIN_PATH)/arm-nacl/lib/*.so* .


STATIC_ARGS:=-DSTATIC -static
DYNAMIC_ARGS:=-DDYNAMIC -L. -Wl,-rpath=.
SO_ARGS:=-DDYNAMIC -fPIC -shared -L.

define BUILD
all: $(1)
$(1) : $(2) $(4) $(MAKEFILE_DEPS) prep_static $(LIBC)
	@echo "**************************************************************"
	@echo REBUILD $(1)
	@echo "**************************************************************"
ifeq ($(suffix $(2)),.c)
	$(CC) -O0 -gdwarf-3 -o $(1) $(2) $(3)
else
	$(CXX) -O0 -gdwarf-3 -o $(1) $(2) $(3)
endif
endef


define BUILD_STATIC
$(call BUILD,$(basename $(1))_static.nexe,$(SRC_ROOT)/$(1),$(STATIC_ARGS) $(2),$(3))

.PHONY : $(basename $(1))_static_test
$(basename $(1))_static_test : $(basename $(1))_static.nexe
	@echo "**************************************************************"
	@echo RUN STATIC TEST $(1)
	@echo "**************************************************************"
	@echo ""
	$(RUN_STATIC_CMD) $(basename $(1))_static.nexe $(4) || (echo "FAILED $(1), non-zero return."; exit 1)
	@echo "**************************************************************"
	@echo "PASSED STATIC $(1)"
	@echo "**************************************************************"
	@echo ""
static_tests: $(basename $(1))_static_test
$(basename $(1))_test : $(basename $(1))_static_test
endef

define BUILD_DYNAMIC
$(call BUILD,$(basename $(1))_dynamic.nexe,$(SRC_ROOT)/$(1),$(DYNAMIC_ARGS) $(2),$(3))

.PHONY : $(basename $(1))_dynamic_test
$(basename $(1))_dynamic_test : $(basename $(1))_dynamic.nexe prep_dynamic
	@echo ""
	@echo "**************************************************************"
	@echo "RUN DYNAMIC TEST $(1)"
	@echo "**************************************************************"
	@echo ""
	$(RUN_DYNAMIC_CMD) $(basename $(1))_dynamic.nexe $(4) || (echo "FAILED $(1), non-zero return."; exit 1)
	@echo "**************************************************************"
	@echo "PASSED DYNAMIC $(1)"
	@echo "**************************************************************"
	@echo ""
dynamic_tests: $(basename $(1))_dynamic_test
$(basename $(1))_test : $(basename $(1))_dynamic_test
endef

define BUILD_SO
$(call BUILD,lib$(basename $(1)).so,$(SRC_ROOT)/$(1),$(SO_ARGS) $(2),$(3),$(4))
endef

define BUILD_BOTH
$(call BUILD_STATIC,$(1),$(2),$(3),$(4))
$(call BUILD_DYNAMIC,$(1),$(2),$(3),$(4))
endef
